package com.logicwin.aibala.action;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipException;

import android.util.Log;

import com.logicwin.aibala.model.androidSWInfo.APKListItem;
import com.logicwin.aibala.services.file.FileConnection;
import com.logicwin.aibala.services.net.NetConnection;
import com.logicwin.aibala.services.xml.XMLParser;
import com.logicwin.aibala.services.zip.ZipUtil;

public class LoadSearchAction {

	private static final String TAG = LoadSearchAction.class.getCanonicalName();
	private static final String getListKeyWords = 
		"http://home.kequan001.com/aibala/getSearchKeywords.php";
	
	private static final String getSearchResult=
		"http://home.kequan001.com/aibala/getSearchResult.php?keyword=";
	public static String[] getSearchKeywords(){
		String resultWords = NetConnection.httpGet(getListKeyWords,null);
		Log.i(TAG,"getSearchList "+resultWords);
		return resultWords.split(",");
	}
	
	public static List<APKListItem> getSearchList(String keyword){
		String searchURL = getSearchResult + "Android";
		String zipPath =  NetConnection.httpGet(searchURL, null);
		Log.i(TAG,"getSearchList URL is "+searchURL);
		if(zipPath.length()==0) return null;
				
		NetConnection.downLoadFile(zipPath, FileConnection.getCurrentPath()+"/temp.zip");
		try {
			ZipUtil.upZipFile(new File(FileConnection.getCurrentPath()+"/temp.zip"), FileConnection.getCurrentPath());
		} catch (ZipException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String xmlPath = zipPath.substring(zipPath.lastIndexOf("/")+1,zipPath.length()-4)+".xml";
		List<APKListItem> apkList = XMLParser.parseList(FileConnection.getCurrentPath()+"/"+xmlPath);
		FileConnection.deleteFile(xmlPath);
		return apkList;
	}
}
